<?php

$check_answers = (bool) get_input('check_answers');

if ($check_answers != false) {
    system_message("check answers");
    
    $_SESSION['isCheck'] = true;
    
    $idListenCurrent = $_SESSION["idListenCurrent"];
    
    $listQuestion = new Question();
    $listQuestion = $listQuestion->loadQuestionByListen($idListenCurrent);
    
    $numberQuestion = count($listQuestion);
    
    $rightNumber = 0;// luu so cau dung trong listen: idlistencurrent
    for ($i = 0; $i<$numberQuestion;++$i)
    {
        $tempQuestion =  new Question();
        $tempQuestion = $listQuestion[$i];
   //     $asdf = get_input($tempQuestion->ID);
        
        
        $tempQuestion->User_Answer = get_input($tempQuestion->ID);
        if($tempQuestion->Right_Answer==$tempQuestion->User_Answer) // neu dap an nguoi dung = ket qua -> tang so cau dung
        {
            ++$rightNumber;
            if($rightNumber==$numberQuestion)
            {
               $listListen  =  $_SESSION["listListen"]; // lay danh sach bai nghe tu listen
               ////////////////-----------Xu ly lam dung cac cau hoi cua 1 listen-------/////////
               for($j = 0 ;$j<count($listListen);$j++) 
               {
                   $tempListen = new Listen();
                   $tempListen = $listListen[$j];
                   if($tempListen->ID==$idListenCurrent) // neu id listen bang voi idlisten hien tai thi gan isdone = 1
                   {
                         $tempListen->isDone = 1;
                         $listListen[$j] = $tempListen;
                         
                   }
               }
               ///////////////---------Xu ly khi lam dung tat ca cac Listen----------//////////
               // neu tat ca cac listen co isdone deu bang 1 thi luu xuong csdl
               $rightNumberListen = 0;
               for($j = 0 ;$j<count($listListen);$j++) 
               {
                   $tempListen = new Listen();
                   $tempListen = $listListen[$j];
                   if($tempListen->isDone==1) 
                       $rightNumberListen++;
               }
               if($rightNumberListen==Count($listListen))
               {
                   // luu xuong csdl
                   $IDUserOnline = elgg_get_logged_in_user_guid();
                   $idLesson = $_SESSION["idLesson"];
                   
                   $status = new Status();
                   $status->SetStatusListen($IDUserOnline, $idLesson);
                   system_message("dung roi");
               }
               
               $_SESSION["listListen"]= $listListen; // luu lai listen
            }
        }
        $listQuestion[$i] = $tempQuestion;
    }
    
    $_SESSION['listQuestionUser'] = $listQuestion;
    
    if (session_is_registered("listQuestionUser")) {
        system_message("Check Answer");
        
        forward(elgg_get_site_url() . "MyCourse/listen");
    } else {
        register_error("can not check answer");
        forward(REFERER); // REFERER is a global variable that defines the previous page
    }
}
?>
